<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="10">
            <a-form-item :label="$t('m.system.quartzjob.entity.jobName')">
              <a-input placeholder="" v-model="queryParam.jobName$lk"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="10">
            <a-form-item :label="$t('m.system.quartzjob.entity.status')">
              <a-select v-model="queryParam.status" placeholder="">
                <a-select-option value="">{{this.$t('m.common.label.all')}}</a-select-option>
                <a-select-option value="0">{{this.$t('m.system.quartzjob.other.status_option1')}}</a-select-option>
                <a-select-option value="-1">{{this.$t('m.system.quartzjob.other.status_option2')}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :md="6" :sm="10">
              <a-form-item :label="$t('m.system.quartzjob.entity.jobClassName')">
                <a-input placeholder="" v-model="queryParam.jobClassName"></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="10">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('m.common.button.query') }}</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">{{ $t('m.common.button.reset') }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? this.$t('m.common.button.fold') : this.$t('m.common.button.expand') }}
                <ych-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">{{ $t('m.common.button.add') }}</a-button>
      <a-button @click="batchDel" type="default" icon="delete">{{ $t('m.common.button.delete') }}</a-button>
      <a-button type="default" icon="download" @click="handleExportXls($t('m.system.quartzjob.other.export_filename'))">{{ $t('m.common.button.export') }}</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel">
        <a-button type="default" icon="import">{{ $t('m.common.button.import') }}</a-button>
      </a-upload>

    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">
        <template slot="serialNum" slot-scope="text,record,index">
          {{(ipagination.current-1) * ipagination.pageSize + parseInt(index) + 1}}
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="resumeJob(record)" v-if="record.status==-1">{{$t('m.system.quartzjob.other.start')}}</a>
          <a @click="pauseJob(record)" v-if="record.status==0">{{$t('m.system.quartzjob.other.stop')}}</a>

          <a-divider type="vertical"/>
          <a @click="handleEdit(record)" >{{ $t('m.common.button.update') }}</a>
          <a-divider type="vertical"/>
                <a-popconfirm :title="$t('m.common.tip.delete_confirm')" @confirm="() => handleDelete(record.id)">
                  <a>{{ $t('m.common.button.delete') }}</a>
                </a-popconfirm>


        </span>

        <!-- 状态渲染模板 -->
        <template slot="customRenderStatus" slot-scope="status">
          <a-tag v-if="status==0" color="green">{{$t('m.system.quartzjob.other.start_status')}}</a-tag>
          <a-tag v-if="status==-1" color="orange">{{$t('m.system.quartzjob.other.stop_status')}}</a-tag>
        </template>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <quartzJob-modal ref="modalForm" @ok="modalFormOk"></quartzJob-modal>
  </a-card>
</template>

<script>

  import QuartzJobModal from './modules/QuartzJobModal'
  import { getAction } from '@/api/manage'
  import { YcListMixin } from '@/mixins/YcListMixin'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'QuartzJobList',
    mixins: [YcListMixin],
    components: {
      QuartzJobModal,
    },
    data () {
      return {
        description: '定时任务在线管理',
        // 查询条件
        queryParam: {},
        // 表头
        columns: [
          {
            title:this.$t('m.common.label.number'),
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            scopedSlots: { customRender: 'serialNum' },
          },
          {
            title: this.$t('m.system.quartzjob.entity.jobName'),
            align: 'center',
            width: 150,
            dataIndex: 'jobName',
            sorter: true
          },
          {
            title: this.$t('m.system.quartzjob.entity.jobClassName'),
            align: 'center',
            width: 250,
            dataIndex: 'jobClassName',
            sorter: true,
            /*            customRender:function (text) {
                          return "*"+text.substring(9,text.length);
                        }*/
          },
          /*          {
                      title: 'cron表达式',
                      align:"center",
                      dataIndex: 'cronExpression'
                    },*/
          /*          {
                      title: '参数',
                      align:"center",
                      width: 150,
                      dataIndex: 'parameter',
                      scopedSlots: {customRender: 'parameterRender'},
                    },*/
          {
            title: this.$t('m.system.quartzjob.entity.description'),
            align: 'center',
            width: 250,
            dataIndex: 'description',
            ellipsis: true,
          },
          {
            title: this.$t('m.system.quartzjob.entity.status'),
            align: 'center',
            dataIndex: 'status',
            width: 80,
            ellipsis: true,
            scopedSlots: {customRender: 'customRenderStatus'},
          },
          {
            title: this.$t('m.common.label.operate'),
            dataIndex: 'action',
            align: 'center',
            width: 180,
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: YchConstant.PREFIX_SYSTEM + '/quartzJob/list',
          delete: YchConstant.PREFIX_SYSTEM + '/quartzJob/delete',
          deleteBatch: YchConstant.PREFIX_SYSTEM + '/quartzJob/deleteBatch',
          pause: YchConstant.PREFIX_SYSTEM + '/quartzJob/pause',
          resume: YchConstant.PREFIX_SYSTEM + '/quartzJob/resume',
          exportXlsUrl: YchConstant.PREFIX_SYSTEM + '/quartzJob/exportExcel',
          importExcelUrl: YchConstant.PREFIX_SYSTEM + '/quartzJob/importExcel',
        },
      }
    },
    computed: {
      importExcelUrl: function () {
        return `${bootConfig[process.env.NODE_ENV].VUE_APP_API}${this.url.importExcelUrl}`
      }
    },

    methods: {

      //筛选需要重写handleTableChange
      handleTableChange (pagination, filters, sorter) {
        //分页、排序、筛选变化时触发
        //TODO 筛选
        if (Object.keys(sorter).length > 0) {
          this.isorter.column = sorter.field
          this.isorter.order = 'ascend' == sorter.order ? 'asc' : 'desc'
        }
        //这种筛选方式只支持单选
        //this.filters.status = filters.status[0];
        this.ipagination = pagination
        this.loadData()
      },
      pauseJob: function (record) {
        var that = this
        //暂停定时任务
        this.$confirm({
          title: this.$t('m.common.title.operate'),
          content: this.$t('m.system.quartzjob.other.stop_tip'),
          onOk: function () {
            getAction(that.url.pause, { jobName: record.jobName }).then((res) => {
              if (res.success) {
                that.messageUtil.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.messageUtil.warning(res.message)
              }
            })
          }
        })

      },
      resumeJob: function (record) {
        var that = this
        //恢复定时任务
        this.$confirm({
          title: this.$t('m.common.title.operate'),
          content: this.$t('m.system.quartzjob.other.start_tip'),
          onOk: function () {
            getAction(that.url.resume, { jobName: record.jobName }).then((res) => {
              if (res.success) {
                that.messageUtil.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.messageUtil.warning(res.message)
              }
            })
          }
        })
      },
    }
  }
</script>
<style lang="less" scoped>

</style>
